﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.ComponentModel;
using System.Reflection;
using System.Web;
using System.Web.Compilation;
using System.IO;
using System.Collections.Specialized;
using System.Linq.Expressions;
using System.Data;

namespace System.Web.Mvc {
    public static class TextInputExtensions {
        #region TextArea
        /// <summary>
        /// Creates a scrollable text box for form input
        /// </summary>
        /// <param name="htmlName">The name and ID of the control</param>
        /// <param name="value">The text for the control</param>
        public static string TextArea(this HtmlHelper helper, string htmlName, object value) {
            //set defaults for rows/cols
            //using Phi :) ratio cols=3*Phi*rows
            return TextArea(helper, htmlName, value, 12, 58, 0, null);
        }
        /// <summary>
        /// Creates a scrollable text box for form input
        /// </summary>
        /// <param name="htmlName">The name and ID of the control</param>
        /// <param name="value">The text for the control</param>
        /// <param name="maxlength">The input limit for the text area</param>
        public static string TextArea(this HtmlHelper helper, string htmlName, object value, int maxlength) {
            //set defaults for rows/cols
            //using Phi :) ratio cols=3*Phi*rows
            return TextArea(helper, htmlName, value, 12, 58, maxlength, null);
        }
        /// <summary>
        /// Creates a scrollable text box for form input
        /// </summary>
        /// <param name="htmlName">The name and ID of the control</param>
        /// <param name="value">The text for the control</param>
        /// <param name="htmlAttributes">Any attributes you want set on the tag. Use anonymous-type declaration for this: new{class=cssclass}</param>
        public static string TextArea(this HtmlHelper helper, string htmlName, object value, object htmlAttributes) {
            //set defaults for rows/cols
            //using Phi :) ratio
            return TextArea(helper, htmlName, value, 12, 58, 0, htmlAttributes);
        }
        /// <summary>
        /// Creates a scrollable text box for form input
        /// </summary>
        /// <param name="htmlName">The name and ID of the control</param>
        /// <param name="value">The text for the control</param>
        /// <param name="maxlength">The input limit for the text area</param>
        /// <param name="htmlAttributes">Any attributes you want set on the tag. Use anonymous-type declaration for this: new{class=cssclass}</param>
        public static string TextArea(this HtmlHelper helper, string htmlName, object value, int maxlength, object htmlAttributes) {
            //set defaults for rows/cols
            //using Phi :) ratio
            return TextArea(helper, htmlName, value, 12, 58, maxlength, htmlAttributes);
        }
        /// <summary>
        /// Creates a scrollable text box for form input
        /// </summary>
        /// <param name="htmlName">The name and ID of the control</param>
        /// <param name="value">The text for the control</param>
        /// <param name="rows">The height</param>
        /// <param name="cols">The width</param>
        public static string TextArea(this HtmlHelper helper, string htmlName, object value, int rows, int cols) {
            return TextArea(helper, htmlName, value, rows, cols,0, null);

        }
        /// <summary>
        /// Creates a scrollable text box for form input
        /// </summary>
        /// <param name="htmlName">The name and ID of the control</param>
        /// <param name="value">The text for the control</param>
        /// <param name="maxlength">The input limit for the text area</param>
        /// <param name="rows">The height</param>
        /// <param name="cols">The width</param>
        public static string TextArea(this HtmlHelper helper, string htmlName, object value, int maxlength, int rows, int cols) {
            return TextArea(helper, htmlName, value, rows, cols, maxlength, null);

        }
        /// <summary>
        /// Creates a scrollable text box for form input
        /// </summary>
        /// <param name="htmlName">The name and ID of the control</param>
        /// <param name="value">The text for the control</param>
        /// <param name="rows">The height</param>
        /// <param name="cols">The width</param>
        /// <param name="maxlength">The input limit for the text area</param>
        /// <param name="htmlAttributes">Any attributes you want set on the tag. Use anonymous-type declaration for this: new{_class=cssclass}</param>
        public static string TextArea(this HtmlHelper helper, string htmlName, object value, int rows, int cols, int maxlength, object htmlAttributes) {
            string textAreaFormat = "<textarea id=\"{0}\"name=\"{0}\" rows=\"{1}\" cols=\"{2}\" {3}>{4}</textarea>";

            string atts = string.Empty;
            if (htmlAttributes != null)
                atts = htmlAttributes.ToAttributeList();

            if (maxlength > 0) {
                atts += "maxlength=\"" + maxlength.ToString() + "\"";
            }



            return string.Format(textAreaFormat, htmlName, rows.ToString(), cols.ToString(), atts, value.ToString().Trim());

        }

        #endregion

        #region Hidden
        /// <summary>
        /// Creates a text box for form input
        /// </summary>
        /// <param name="htmlName">The name and ID of the control</param>
        /// <param name="value">The text for the control</param>
        /// <param name="htmlAttributes">Any attributes you want set on the tag. Use anonymous-type declaration for this: new{class=cssclass}</param>
        public static string Hidden(this HtmlHelper helper, string htmlName, object value) {
            return TextBox(helper, htmlName, value.ToString(), 20,0, new { type = "hidden" });
        }

        #endregion

        #region TextBox
        /// <summary>
        /// Creates a text box for form input
        /// </summary>
        /// <param name="htmlName">The name and ID of the control</param>
        /// <param name="value">The text for the control</param>
        /// <param name="htmlAttributes">Any attributes you want set on the tag. Use anonymous-type declaration for this: new{class=cssclass}</param>
        public static string TextBox(this HtmlHelper helper, string htmlName, object value, object htmlAttributes) {
            return TextBox(helper, htmlName, value, 20,0, htmlAttributes);
        }

        /// <summary>
        /// Creates a text box for form input
        /// </summary>
        /// <param name="htmlName">The name and ID of the control</param>
        public static string TextBox(this HtmlHelper helper, string htmlName) {
            return TextBox(helper, htmlName, "", 20,0, null);
        }


        /// <summary>
        /// Creates a text box for form input
        /// </summary>
        /// <param name="htmlName">The name and ID of the control</param>
        /// <param name="value">The text for the control</param>
        public static string TextBox(this HtmlHelper helper, string htmlName, object value) {
            return TextBox(helper, htmlName, value, 20,0, null);
        }

        /// <summary>
        /// Creates a text box for form input
        /// </summary>
        /// <param name="htmlName">The name and ID of the control</param>
        /// <param name="size">The size of the textbox</param>
        public static string TextBox(this HtmlHelper helper, string htmlName, int size) {
            return TextBox(helper, htmlName, "", size,0, null);
        }
        /// <summary>
        /// Creates a text box for form input
        /// </summary>
        /// <param name="htmlName">The name and ID of the control</param>
        /// <param name="size">The size of the textbox</param>
        /// <param name="maxlength">The maximum characters allowed in the textbox</param>
        public static string TextBox(this HtmlHelper helper, string htmlName, int size, int maxlength) {
            return TextBox(helper, htmlName, "", size, maxlength, null);
        }

        /// <summary>
        /// Creates a text box for form input
        /// </summary>
        /// <param name="htmlName">The name and ID of the control</param>
        /// <param name="value">The text for the control</param>
        /// <param name="size">The size of the textbox</param>
        public static string TextBox(this HtmlHelper helper, string htmlName, object value, int size) {
            return TextBox(helper, htmlName, value, size,0, null);
        }

        /// <summary>
        /// Creates a text box for form input
        /// </summary>
        /// <param name="htmlName">The name and ID of the control</param>
        /// <param name="value">The text for the control</param>
        /// <param name="size">The size of the textbox</param>
        /// <param name="maxlength">The maximum characters allowed in the textbox</param>
        public static string TextBox(this HtmlHelper helper, string htmlName, object value, int size, int maxlength) {
            return TextBox(helper, htmlName, value, size, maxlength, null);
        }
        /// <summary>
        /// Creates a text box for form input
        /// </summary>
        /// <param name="htmlName">The name and ID of the control</param>
        /// <param name="value">The text for the control</param>
        /// <param name="size">The size of the textbox</param>
        /// <param name="maxlength">The maximum characters allowed in the textbox</param>
        /// <param name="htmlAttributes">Any attributes you want set on the tag. Use anonymous-type declaration for this: new{class=cssclass}</param>
        public static string TextBox(this HtmlHelper helper, string htmlName, object value, int size, int maxlength, object htmlAttributes) {
            string textBoxFormat = "<input id=\"{0}\" name=\"{0}\" size=\"{1}\" {2} value=\"{3}\">";

            string atts = string.Empty;
            if (htmlAttributes != null)
                atts = htmlAttributes.ToAttributeList();

            if (maxlength > 0) {
                atts += "maxlength=\"" + maxlength.ToString() + "\"";
            }

            return string.Format(textBoxFormat, htmlName, size.ToString(), atts, value.ToString());

        }
        #endregion

        #region Password

        /// <summary>
        /// Creates a password entry field
        /// </summary>
        /// <param name="htmlName">The name and ID of the control</param>
        /// <param name="value">The text for the control</param>
        public static string Password(this HtmlHelper helper, string htmlName, object value) {
            return Password(helper, htmlName, 20, value, null);
        }
        /// <summary>
        /// Creates a password entry field
        /// </summary>
        /// <param name="htmlName">The name and ID of the control</param>
        /// <param name="size">The size of the textbox</param>
        /// <param name="value">The text for the control</param>
        public static string Password(this HtmlHelper helper, string htmlName, int size, object value) {
            return Password(helper, htmlName, 20, value, null);
        }
        /// <summary>
        /// Creates a password entry field
        /// </summary>
        /// <param name="htmlName">The name and ID of the control</param>
        public static string Password(this HtmlHelper helper, string htmlName) {
            return Password(helper, htmlName, 20, "", null);
        }
        /// <summary>
        /// Creates a password entry field
        /// </summary>
        /// <param name="htmlName">The name and ID of the control</param>
        /// <param name="size">The size of the textbox</param>
        public static string Password(this HtmlHelper helper, string htmlName, int size) {
            return Password(helper, htmlName, size, "", null);
        }

        /// <summary>
        /// Creates a password entry field
        /// </summary>
        /// <param name="htmlName">The name and ID of the control</param>
        /// <param name="size">The size of the textbox</param>
        /// <param name="value">The text for the control</param>
        /// <param name="htmlAttributes">Any attributes you want set on the tag. Use anonymous-type declaration for this: new{class=cssclass}</param>
        public static string Password(this HtmlHelper helper, string htmlName, int size, object value, object htmlAttributes) {
            string passwordFormat = "<input type=password id=\"{0}\" name=\"{0}\" size={1} {2} value=\"{3}\"/>";

            string atts = string.Empty;
            if (htmlAttributes != null)
                atts = htmlAttributes.ToAttributeList();

            return string.Format(passwordFormat, htmlName, size.ToString(), atts, value.ToString());

        }
        #endregion

    }
}
